﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using YJKOpeningCommon.DataEnum;

namespace YJKOpeningCommon.Public
{
	public partial class SelecteXmlPathForm : ConfigDataBaseForm
	{
		public string XmlPathName
		{
			get
			{
				return this.m_strXmlFilePath;
			}
			set
			{
				this.m_strXmlFilePath = value;
			}
		}

		public SelecteXmlPathForm(ESelecteXmlPathForm eSelecteXmlPathForm)
		{
			this.m_eSelecteXmlPathForm = eSelecteXmlPathForm;
			this.InitializeComponent();
			this.XmlPathName = this.GetXmlPathName();
			base.SetFormLocation(this.m_strXmlPathLocation);
		}

		private void btnSelectPath_Click(object sender, EventArgs e)
		{
			if (this.m_eSelecteXmlPathForm == ESelecteXmlPathForm.eDesignInput)
			{
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.InitialDirectory = this.m_strXmlFilePath.Remove(this.m_strXmlFilePath.LastIndexOf('\\') + 1);
				saveFileDialog.Filter = "xml files (*.xml)|*.xml";
				saveFileDialog.Title = "提资文件路径";
				saveFileDialog.RestoreDirectory = true;
				string xmlPathName = this.GetXmlPathName();
				int num = xmlPathName.LastIndexOf("\\");
				xmlPathName.Substring(num + 1);
				saveFileDialog.FileName = this.txtXmlPath.Text;
				if (saveFileDialog.ShowDialog() == DialogResult.OK)
				{
					this.m_strXmlFilePath = saveFileDialog.FileName;
					this.txtXmlPath.Text = this.m_strXmlFilePath;
					this.SaveXmlPathName();
					return;
				}
			}
			else if (this.m_eSelecteXmlPathForm == ESelecteXmlPathForm.eCheckOpening)
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.InitialDirectory = this.m_strXmlFilePath.Remove(this.m_strXmlFilePath.LastIndexOf("\\"));
				openFileDialog.Filter = "xml files (*.xml)|*.xml";
				openFileDialog.Title = "查看洞口文件路径";
				openFileDialog.RestoreDirectory = true;
				openFileDialog.FileName = this.GetXmlPathName();
				if (openFileDialog.ShowDialog() == DialogResult.OK)
				{
					this.m_strXmlFilePath = openFileDialog.FileName;
					this.txtXmlPath.Text = this.m_strXmlFilePath;
					this.SaveXmlPathName();
					return;
				}
			}
			else
			{
				OpenFileDialog openFileDialog2 = new OpenFileDialog();
				openFileDialog2.InitialDirectory = this.m_strXmlFilePath.Remove(this.m_strXmlFilePath.LastIndexOf("\\"));
				openFileDialog2.Filter = "xml files (*.xml)|*.xml";
				openFileDialog2.Title = "开洞文件路径";
				openFileDialog2.RestoreDirectory = true;
				openFileDialog2.FileName = this.txtXmlPath.Text;
				if (openFileDialog2.ShowDialog() == DialogResult.OK)
				{
					this.m_strXmlFilePath = openFileDialog2.FileName;
					this.txtXmlPath.Text = this.m_strXmlFilePath;
					this.SaveXmlPathName();
				}
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.m_strXmlFilePath = this.txtXmlPath.Text;
			base.Close();
		}

		private void SelecteXmlPathForm_Load(object sender, EventArgs e)
		{
			if (this.m_eSelecteXmlPathForm == ESelecteXmlPathForm.eDesignInput)
			{
				this.Text = "提资文件路径";
			}
			else if (this.m_eSelecteXmlPathForm == ESelecteXmlPathForm.eCreateOpening)
			{
				this.Text = "开洞文件路径";
			}
			else
			{
				this.Text = "查看洞口文件路径";
			}
			string text = string.Concat(new string[]
			{
				Product.UserDataLocation,
				"\\",
				Environment.UserName,
				"_",
				DateTime.Now.ToLongDateString().ToString(),
				DateTime.Now.ToLongTimeString().ToString().Replace(":", "："),
				"_提资.xml"
			});
			if (this.m_eSelecteXmlPathForm == ESelecteXmlPathForm.eDesignInput)
			{
				this.txtXmlPath.Text = text;
				return;
			}
			this.txtXmlPath.Text = this.XmlPathName;
		}

		private string GetXmlPathName()
		{
			try
			{
				if (File.Exists(this.m_strSaveXmlPath))
				{
					XmlDocument xmlDocument = new XmlDocument();
					XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
					xmlReaderSettings.IgnoreComments = true;
					XmlReader xmlReader = XmlReader.Create(this.m_strSaveXmlPath, xmlReaderSettings);
					xmlDocument.Load(xmlReader);
					XmlNode xmlNode = xmlDocument.SelectSingleNode("XmlPathName");
					this.m_strXmlFilePath = xmlNode.InnerText;
					xmlReader.Close();
				}
				else
				{
					this.m_strXmlFilePath = this.m_strOpeningDefault;
				}
			}
			catch (Exception)
			{
				this.m_strXmlFilePath = this.m_strOpeningDefault;
			}
			return this.m_strXmlFilePath;
		}

		private bool SaveXmlPathName()
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				if (File.Exists(this.m_strSaveXmlPath))
				{
					XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
					xmlReaderSettings.IgnoreComments = true;
					XmlReader xmlReader = XmlReader.Create(this.m_strSaveXmlPath, xmlReaderSettings);
					xmlDocument.Load(xmlReader);
					xmlDocument.SelectSingleNode("XmlPathName").InnerText = this.m_strXmlFilePath;
					xmlReader.Close();
				}
				else
				{
					XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
					xmlDocument.AppendChild(newChild);
					XmlNode xmlNode = xmlDocument.CreateElement("XmlPathName");
					xmlNode.InnerText = this.m_strXmlFilePath;
					xmlDocument.AppendChild(xmlNode);
				}
				xmlDocument.Save(this.m_strSaveXmlPath);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void SelecteXmlPathForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			base.Location.X.ToString();
			base.Location.Y.ToString();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				if (File.Exists(this.m_strXmlPathLocation))
				{
					XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
					xmlReaderSettings.IgnoreComments = true;
					XmlReader xmlReader = XmlReader.Create(this.m_strXmlPathLocation, xmlReaderSettings);
					xmlDocument.Load(xmlReader);
					XmlElement documentElement = xmlDocument.DocumentElement;
					documentElement.SetAttribute("LocationX", base.Location.X.ToString());
					documentElement.SetAttribute("LocationY", base.Location.Y.ToString());
					xmlReader.Close();
				}
				else
				{
					XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
					xmlDocument.AppendChild(newChild);
					XmlElement xmlElement = xmlDocument.CreateElement("HistoryData");
					xmlDocument.AppendChild(xmlElement);
					xmlElement.SetAttribute("LocationX", base.Location.X.ToString());
					xmlElement.SetAttribute("LocationY", base.Location.Y.ToString());
				}
				xmlDocument.Save(this.m_strXmlPathLocation);
			}
			catch (Exception)
			{
				throw;
			}
		}

		private ESelecteXmlPathForm m_eSelecteXmlPathForm;

		private string m_strXmlFilePath;

		private string m_strSaveXmlPath = Product.UserDataLocation + "\\SelecteXmlPathForm.xml";

		private string m_strOpeningDefault = string.Concat(new string[]
		{
			Product.UserDataLocation,
			"\\",
			Environment.UserName,
			"_",
			DateTime.Now.ToLongDateString().ToString(),
			DateTime.Now.ToLongTimeString().ToString().Replace(":", "："),
			"_提资.xml"
		});

		private string m_strXmlPathLocation = Product.UserDataLocation + "\\SelecteXmlPathFormLocation.xml";
	}
}
